import json

import pymongo
import requests
import execjs
import pprint


class Rank:
    def __init__(self):
        self.mongo_client = pymongo.MongoClient(host='localhost', port=27017)
        self.collection = self.mongo_client['py_spider']['rank_list']
        self.headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Referer": "https://www.chinaindex.net/ranklist/5",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
        }
        with open("艾漫指数.js", encoding="utf-8") as f:
            self.js_code = f.read()

    def get_movie(self):
        url = "https://www.chinaindex.net/iIndexMobileServer/mobile/movie/objectFansRank"

        params = {
            # varietylist 综艺榜，movielist 电影榜
            "channel": "movielist",
        }
        print("请求获取电影榜...........")
        sign = execjs.compile(self.js_code).call("getSign", params)
        params['sign'] = sign

        response = requests.get(url, headers=self.headers, params=params)
        res = response.json()
        result = execjs.compile(self.js_code).call("decrypt", res['data'], res['lastFetchTime'])
        for movie in result['listOfRank']:
            item = dict()
            item['channel'] = "电影榜"
            item['title'] = movie['object_name']
            item['type'] = movie['object_career']
            item['time'] = movie['publish_time']
            item['fans_num'] = movie['total_fans_num']
            item['rank'] = movie['rank']
            self.collection.insert_one(item)
            print("入库成功：", item)

    def get_tv(self):
        url = "https://www.chinaindex.net/iIndexMobileServer/teleplay/rank/waiting/fans"
        params = {
        }

        print("请求获取剧集版...........")
        sign = execjs.compile(self.js_code).call("getSign", params)
        params['sign'] = sign

        response = requests.get(url, headers=self.headers, params=params)
        res = response.json()
        result = execjs.compile(self.js_code).call("decrypt", res['data'], res['lastFetchTime'])
        for tv in result['rank_list']:
            item = dict()
            item['channel'] = "剧集榜"
            item['title'] = tv['object_info']['name']
            item['type'] = json.dumps(tv['object_info']['tags'], ensure_ascii=False)
            item['time'] = tv['object_info']['publish_date']
            item['fans_num'] = tv['fans_num']
            item['rank'] = tv['rank']
            self.collection.insert_one(item)
            print("入库成功：", item)

    def get_variety(self):
        url = "https://www.chinaindex.net/iIndexMobileServer/mobile/entertainment/objectFansRank"

        params = {
            # varietylist 综艺榜，movielist 电影榜
            "channel": "varietylist",
        }

        print("请求获取综艺榜...........")
        sign = execjs.compile(self.js_code).call("getSign", params)
        params['sign'] = sign
        response = requests.get(url, headers=self.headers, params=params)

        res = response.json()
        result = execjs.compile(self.js_code).call("decrypt", res['data'], res['lastFetchTime'])
        for variety in result['listOfRank']:
            item = dict()
            item['channel'] = "综艺榜"
            item['title'] = variety['object_name']
            item['type'] = variety['object_career']
            item['time'] = variety['publish_time']
            item['fans_num'] = variety['total_fans_num']
            item['rank'] = variety['rank']
            self.collection.insert_one(item)
            print("入库成功：", item)


if __name__ == '__main__':
    m = Rank()
    m.get_movie()
    m.get_variety()
    m.get_tv()
